package info.motteke.qjdbc.column.variable;

import static java.util.Calendar.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Calendar;

public final class TimeVariable extends Variable {

    private long time;

    private boolean set = false;

    public TimeVariable() {

    }

    public TimeVariable(java.util.Date time) {
        is(time);
    }

    public void is(java.util.Date time) {
        Calendar cal = getInstance();
        cal.set(YEAR, 1970);
        cal.set(MONTH, JANUARY);
        cal.set(DATE, 1);

        this.time = cal.getTimeInMillis();
        set = true;
    }

    @Override
    void putValue(PreparedStatement pstmt, int index) throws SQLException {
        if (!set) {
            throw new IllegalStateException();
        }
        pstmt.setTime(index, new Time(time));
    }

}
